/**
 * Created by way on 16/9/21.
 */

import cfg from './config';
import Router from './lib/krouter';
import {setTitle, urlParam, session, getHash,getBaseUrl} from './lib/kutil';

import {get, docker,myalertp,myget} from './util/api';
//import {getView, get, post ,myalertp,prototype,core,Converter,BusinessTravelTool,Dept,MGOpt,MemberOpt,OrderSubmitOpt,RsaTool,mycheckuser,SetCookie,getCookieVal,GetCookie} from './util/api';// myalertp 封装的 alert提示弹层

import Join from './flightmb/join';
import City from './flightmb/city';
import Adate from './flightmb/adate';
import Detail from './flightmb/detail';
import Picktime from './flightmb/picktime';
import Product from './flightmb/product';
import Allmytickes from './flightmb/allmytickes';
import Passenger from './flightmb/passenger';
import Changepassenger from './flightmb/changepassenger';
import Myhalinkp from './flightmb/mychalinkp';
import CChangelinkp from './flightmb/changelinkp';
import Book from './flightmb/book';
import Orderpage from './flightmb/Order';
import Contactpeople from './flightmb/contactpeople';
import Changeadd from './flightmb/changeadd';
import  Trip from './flightmb/Trip';
import  Allmybook from './flightmb/allmybook';
import  Orderd from './flightmb/orderd';



const router = new Router({
  container: '#dvContainer'
});


document.ready(() => {

//global.flightUrl = "http://106.75.131.58:8015"; //  机票的绝对地址
//global.flightUrlprice = "http://121.52.212.39:83"; //  查询航班最低价绝对地址 https://cos.uair.cn/mb/img/
//global.imgUrl = "https://cos.uair.cn/mb/"; //  腾讯地址图片






  router.push(new Join())
    .push(new City())
    .push(new Detail())
    .push(new Picktime())
    .push(new Product())
    .push(new Allmytickes())
    .push(new Passenger())
    .push(new Changepassenger())
    .push(new Myhalinkp())
    .push(new CChangelinkp())
    .push(new Book())
    .push(new Orderpage())
    .push(new Contactpeople())
    .push(new Changeadd())
    .push(new Trip())
    .push(new Allmybook())
    .push(new Orderd())
    .push(new Adate());

   //获取地址穿参
  console.log('传输地址');
  var url =window.location.href;
  //console.log(url);
  var bsurl = getBaseUrl(window.location.href);
  console.log(bsurl)
  var myurlarr =bsurl.split('?')
  var  myurl = myurlarr[0];

  console.log(myurl)
  var  xhurl  =bsurl.slice(-1);

  console.log(xhurl)//  初始的时候 未带  entry_code  会截取 aspx的最后一位 为x
   // a  为 我的机票 登陆返回参数
   // p 为产品页面返回参数
  // 其他登陆超时的时候 会自动返回初始页面

  // 剪掉 地址参数
  //window.location.href =myurl;


    var datap =localStorage.getItem("allbookdatastr");
    console.log(datap);
    var allbookdatastr = '';
    if(datap){
        allbookdatastr = JSON.parse( datap );//取回students变量 把字符串转换成JSON对象
    }

    console.log(allbookdatastr)



    //myalertp('dvContainer','账号已经绑定，即将默认登陆！',function (){
    //    goway();
    //})




    //  路由跳转
    goway();


    function goway() {
        // 初始路由
        if(xhurl == 'a'){// 我的机票登陆
            $.router.go('/flightmb/allmytickes', {}, true);

        }else if(xhurl == 'x' || xhurl == 's' || xhurl == 'c'){// 直接进入aspx   x新打开jion  s 乘机人返回的
            $.router.go('/flightmb/join',{linkp:1},true);
        }else if(xhurl == 'p' && allbookdatastr){ //产品页面 登陆
            $.router.go('/flightmb/book',allbookdatastr,true)
            localStorage.setItem("allbookdatastr",'');//  将 cookie清空

        }else if(xhurl == 'b' && allbookdatastr){//  预定 需要登陆

            $.router.go('/flightmb/detail',allbookdatastr,true)
            localStorage.setItem("allbookdatastr",'');//  将 cookie清空


        }else if(xhurl == 't') {//  差旅传数据
            console.log('差旅航程数据填充')

            //console.log(GetQueryString('splace'))//
            $.router.go('/flightmb/join',{linkp:5,joindata:GetQueryString(myurlarr[1])},true);
        }else{ // 刷新卡的 解决  刷新了就要返回 首页的
            $.router.go('/flightmb/join',{linkp:1},true);
        }

    }









});
//  默认 登陆函数
function checklogin() {
    var  userid = '';
    var  loginId ='';
    //alert(window.navigator.userAgent)

    //myget('/icbc/zyService.ashx','act=loginOrder&userId=201611210050159038&loginId=kenrecall&OrderID=1',true, function (err, res) {
    //    console.log(err)
    //    console.log(res)
    //    if(err){
    //        //myalertp('dvContainer','出错了，默认登陆失败')
    //        console.log('默认登陆失败！')
    //    }else{
    //        console.log('默认登陆成功！')
    //        console.log(res)
    //    }
    //});


}



function creatScript(url){
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", flightUrl + url);
  var script1 = document.getElementsByTagName('body')[0];
  script1.appendChild(script)
}

function creatScripth(url){
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", flightUrl + url);
  var script1 = document.getElementsByTagName('head')[0];
  script1.appendChild(script)
}



function GetQueryString(durl){
   var  myarr = durl.split('&');
   var json={
       sphone:getd(myarr[0]),
       sname:getd(myarr[1]),
       timef:getd(myarr[2]),
       etime:getd(myarr[3]),
       ctyf:getd(myarr[4]),
       ctyt:getd(myarr[5]),
       timet:''

  }
   console.log(json)

  function getd(data) {
      return decodeURI(data.split('=')[1])
  }

  return json

}

